﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using Microsoft.Phone.Controls;
using System.IO.IsolatedStorage;
using System.IO;
using System.Windows.Media.Imaging;
using OnlineVideos.Library;
using Common.Library;
using OnlineVideos.Data;
using OnlineVideos.UI;
using System.Threading.Tasks;
using OnlineVideos.Entities;

namespace OnlineVideos
{
    public partial class DownLoadedVideos : PhoneApplicationPage
    {
        IsolatedStorageFile isoStore = IsolatedStorageFile.GetUserStoreForApplication();
        public static List<string> filename = new List<string>();
        public static int filecount;
        public static int fileid = 1;
        MediaStreamSource mss;
        double x;
        public string cn = "";
        int count = 0;
        public DownLoadedVideos()
        {
            InitializeComponent();
            LoadPivotThemes();
        }
        private void LoadPivotThemes()
        {
            //imgTitle.Style = Application.Current.Resources["PivotTitle"] as Style; ;
            //LayoutRoot.Background = Application.Current.Resources["bipivot"] as ImageBrush;

        }
        protected override void OnNavigatedFrom(System.Windows.Navigation.NavigationEventArgs e)
        {
            try
            {
                FlurryWP8SDK.Api.EndTimedEvent("DownLoadedVideos Page");
            }
            catch (Exception ex)
            {

                Exceptions.SaveOrSendExceptions("Exception in OnNavigatedFrom Method In DownLoadedVideos.cs file.", ex);
            }
        }
        protected override void OnNavigatedTo(System.Windows.Navigation.NavigationEventArgs e)
        {
            try
            {
                FlurryWP8SDK.Api.LogPageView();
            }
            catch (Exception ex)
            {

                Exceptions.SaveOrSendExceptions("Exception in OnNavigatedTo Method In DownLoadedVideos.cs file.", ex);
            }
        }
        private void PhoneApplicationPage_Loaded(object sender, RoutedEventArgs e)
        {
            try
            {
                FlurryWP8SDK.Api.LogEvent("DownLoadedVideos Page", true);
                string id = "";
                if (NavigationContext.QueryString.TryGetValue("id", out id))
                {
                    loadMediaFile(id);
                    var gallery1 = Task.Run(async () => await Constants.connection.Table<ShowLinks>().Where(i => i.UrlType == ".wmv" || i.UrlType == ".mp4").ToListAsync()).Result;
                    cn = gallery1.Count().ToString();
                }
            }
            catch (Exception ex)
            {
                
                  Exceptions.SaveOrSendExceptions("Exception in PhoneApplicationPage_Loaded Method In DownLoadedVideos.cs file.", ex);
            }

        }

        private void loadMediaFile(string id)
        {
            try
            {
                string path = "";
                int showid = Convert.ToInt32(id);
                var xquery = Task.Run(async () => await Constants.connection.Table<ShowLinks>().Where(i => i.ShowID == showid).ToListAsync()).Result;
                foreach (var itm in xquery)
                {
                    path = itm.LinkUrl;

                }
                using (var isfs = new IsolatedStorageFileStream(path, FileMode.Open, isoStore))
                {
                    this.mediaElement1.SetSource(isfs);
                }
                if (count == 0)
                {
                    mediaElement1.AutoPlay = true;
                    mediaElement1.Play();
                    count++;
                }

            }
            catch (Exception ex)
            {
                
              Exceptions.SaveOrSendExceptions("Exception in loadMediaFile Method In DownLoadedVideos.cs file.", ex);
            }
        }

        private void imgTitle_MouseEnter(object sender, MouseEventArgs e)
        {
            NavigationService.Navigate(NavigationHelper.MainPanoramaPage);
        }

      

        private void Play_Click(object sender, EventArgs e)
        {
           // play.IconUri = new Uri("/Images/appbar.transport.pause.rest.png");
            mediaElement1.Play();
        }

       
        public void dis(string c)
        {
            string path = "";
            string id = "";
            var xquery = Task.Run(async () => await Constants.connection.Table<ShowLinks>().Where(i => i.LinkUrl == c).ToListAsync()).Result; 
            foreach (var itm in xquery)
            {
                path = itm.LinkUrl;
                id =(itm.ShowID).ToString();

            }
            loadMediaFile(id);
            //using (var isfs = new IsolatedStorageFileStream(path, FileMode.Open, isoStore))
            //{
            //    this.mediaElement1.SetSource(isfs);
            //}
            //mediaElement1.Play();


        }
        private void mediaElement1_ManipulationCompleted(object sender, ManipulationCompletedEventArgs e)
        {
            try
            {
                mediaElement1.AutoPlay = false;
                mediaElement1.Stop();
                mediaElement1.Source = null;
                var gallery = Task.Run(async () => await Constants.connection.Table<ShowLinks>().Where(i => i.UrlType == ".wmv" || i.UrlType == ".mp4").ToListAsync()).Result;
                foreach (var gal in gallery)
                {
                    filename.Add(gal.LinkUrl);
                }
                if (x < 0)
                {
                    if (filecount == int.Parse(cn) - 1)
                    {
                        fileid = 0;
                        dis(filename[fileid].ToString());
                        filecount = (fileid + 1);
                        fileid++;

                    }
                    else
                    {
                        filecount = Convert.ToInt32(fileid) + 1;
                        dis(filename[fileid].ToString());
                        filecount = fileid;
                        fileid++;
                    }
                }
                if (x > 0)
                {
                    if (filecount.ToString() == "0")
                    {
                        fileid = int.Parse(cn);
                        dis(filename[fileid].ToString());
                        filecount = (fileid - 1);
                        fileid--;

                    }
                    else
                    {
                        filecount = Convert.ToInt32(fileid) - 1;
                        dis(filename[fileid].ToString());
                        filecount = (fileid);
                        fileid--;
                    }
                }
            }
            catch (Exception ex)
            {
                
                 Exceptions.SaveOrSendExceptions("Exception in mediaElement1_ManipulationCompleted Method In DownLoadedVideos.cs file.", ex);
            }
        }

        private void mediaElement1_ManipulationDelta(object sender, ManipulationDeltaEventArgs e)
        {
            try
            {
                x = e.DeltaManipulation.Translation.X;
            }
            catch (Exception ex)
            {
                
               Exceptions.SaveOrSendExceptions("Exception in mediaElement1_ManipulationDelta Method In DownLoadedVideos.cs file.", ex);
            }
        }
    }
}